package cn.com.victorysoft.ylgl.service.commons.mybatis.paging.dialect;

import cn.com.victorysoft.ylgl.service.commons.mybatis.paging.CountHelper;

/***
 * @author gechaoqing
 */
public abstract class AbstractDialect
{
    /***
     * is support limit
     * @return isSupportLimit
     */
    public abstract boolean supportsLimit();

    /***
     * limit SQL string
     * @param paramString string
     * @param paramInt1 int
     * @param paramInt2 int
     * @return limit String
     */
    public abstract String getLimitString(String paramString, int paramInt1, int paramInt2);

    public String getCountString(String sql)
    {
        String newsql;
        if ((CountHelper.containOrder(sql)) && (!CountHelper.containOver(sql))) {
            newsql = getNonOrderByPart(sql);
        } else {
            newsql = sql;
        }
        return "select count(1) from (" + newsql.trim() + ") tmp_count";
    }

    protected String getLineSql(String sql)
    {
        return sql.replaceAll("[\r\n]", " ").replaceAll("\\s{2,}", " ");
    }

    private static String getNonOrderByPart(String sql)
    {
        return CountHelper.removeOrders(sql);
    }
}
